package org.yanghaokun.pay.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yanghaokun.basic.exception.BusinessException;
import org.yanghaokun.basic.service.impl.BaseServiceImpl;
import org.yanghaokun.order.service.IProductOrderService;
import org.yanghaokun.pay.domain.AlipayInfo;
import org.yanghaokun.pay.domain.PayBill;
import org.yanghaokun.pay.mapper.AlipayInfoMapper;
import org.yanghaokun.pay.mapper.PayBillMapper;
import org.yanghaokun.pay.service.IPayBillService;
import org.yanghaokun.pay.utils.AlipayUtils;

@Service
public class PayBillServiceImpl extends BaseServiceImpl<PayBill> implements IPayBillService {

    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private IProductOrderService productOrderService;

    /**
     * 我们需要拉起请求界面所以返回的就相当于是一个字符窜
     * 相当于获取一个地址来请求
     * @param payBill
     * @return
     */
    @Override
    public String payRequest(PayBill payBill) {
        //首先我们先进来看是否这个是否有订单，不然空指针
        if (payBill == null){
            throw new BusinessException("你还没有订单，请先创建");
        }
        //我们再通过唯一的支付订单号来找支付订单
        PayBill payBill1 = payBillMapper.loadByUnionPaySn(payBill.getUnionPaySn());
        //再次判断是否为空
        if (payBill1 == null) {
            throw new BusinessException("你还没有订单，请先创建");
        }

        //我们再来看看是什么支付方式，来选择对应的支付请求
        Long payChannel = payBill.getPayChannel();
        switch(payChannel.intValue()) {
            //支付宝
            case 1 :
                //我们发起支付报的支付请求，我们看看发起支付宝请求需要些什么
                //看了得知我们需要支付宝的支付信息还有付款订单
                //获取支付宝信息
                AlipayInfo info = alipayInfoMapper.loadByShopId(payBill.getShop_id());
                //参数都准备好了调用工具类拉其支付请求
                return AlipayUtils.pay(info,payBill);

            case 2 :
                //@TODO
                break;
            case 3:
                //@TODO
                break;
            default:
                //@TODO
                break;
        }
        return null;
    }

    @Override
    public PayBill queryByUnionPaySn(String unionPaySn) {
        return payBillMapper.loadByUnionPaySn(unionPaySn);
    }
}
